<?php
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';

$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 10;
$offset = ($page - 1) * $limit;

$sql = "SELECT * FROM notices 
        WHERE status = 1 
        ORDER BY type DESC, created_at DESC 
        LIMIT ?, ?";

$stmt = $conn->prepare($sql);
$stmt->bind_param("ii", $offset, $limit);
$stmt->execute();
$result = $stmt->get_result();

$notices = [];
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // 格式化时间
        $row['created_at'] = date('Y-m-d H:i', strtotime($row['created_at']));
        // 截取预览内容
        $row['content'] = mb_substr(strip_tags($row['content']), 0, 100) . '...';
        $notices[] = $row;
    }
}

echo json_encode([
    'status' => 'success',
    'notices' => $notices
]);

$stmt->close();
$conn->close();
?> 